package com.example.reggie.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.reggie.bean.*;
import com.example.reggie.mapper.OrdersMapper;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
public class OrdersService extends ServiceImpl<OrdersMapper, Orders> implements IService<Orders> {
    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private UserService userService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private AddressBookService addressBookService;

    @Transactional
    public void submit(Orders orders){
        User user=userService.getById(orders.getUserId());
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper=new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,orders.getUserId());
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
        AddressBook addressBook=addressBookService.getById(orders.getAddressBookId());
        IdWorker idWorker=new IdWorker();
        Long orderId=idWorker.getId();
        orders.setNumber(String.valueOf(orderId));//订单号
        orders.setStatus(2);//订单状态
        orders.setUserId(user.getId());//下单用户id
        orders.setOrderTime(LocalDateTime.now());//下单时间
        orders.setCheckoutTime(LocalDateTime.now());//结账时间
        orders.setPayMethod(1); //支付方式 1微信，2支付宝
        orders.setUserName(user.getName());//用户名
        orders.setPhone(addressBook.getPhone());//手机号
        orders.setAddress(addressBook.getDetail());//地址
        orders.setConsignee(addressBook.getConsignee());//收货人
        AtomicInteger atomicInteger=new AtomicInteger(0);
        List<OrderDetail> orderDetailList=shoppingCartList.stream().map(item->{
            atomicInteger.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
            OrderDetail orderDetail =new OrderDetail();
            orderDetail.setOrderId(orderId);
            orderDetail.setAmount(item.getAmount());
            orderDetail.setDishFlavor(item.getDishFlavor());
            orderDetail.setImage(item.getImage());
            orderDetail.setDishId(item.getDishId());
            orderDetail.setSetmealId(item.getSetmealId());
            return orderDetail;
        }).collect(Collectors.toList());
        orders.setAmount(new BigDecimal(atomicInteger.get()));

        this.save(orders);
        orderDetailService.saveBatch(orderDetailList);
    }
}
